﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using JSC.Portal.Data;
using JSC.Portal.Web;
using NHibernate;
using JSC.Portal.Entities;
using JSC.Core;
using JSC.Core.Data;
using JSC.Data.NHibernate;
using JSC.Portal.Web.Controllers;
using JSC.Portal.Module;
using System.Collections.Specialized;

namespace JSC.Portal.Web.App.Modules.StaticContents
{
    [Authorize(Roles = "Administrator,SiteAdministrator,Editor")]
    public class StaticContentsController : ModuleController<StaticContentModule>
    {
        public ActionResult Edit(int sectionid)
        {
            LoadModule(sectionid);
            Module.Load();
            return PartialView("StaticContentEditView", Module.StaticContent);
        }

        [ValidateInput(false)]
        public JsonResult SaveOrUpdate(int sectionid, string title, string content, string returnUrl)
        {
            LoadModule(sectionid);
            Module.Load();
            using (ITransaction trans = NHibernateSession.Current.BeginTransaction())
            {
                Module.StaticContent.Section = Section;
                Module.StaticContent.Title = title;
                Module.StaticContent.Content = content;
                Module.StaticContent.SetTrackedData(User.Identity as PortalUser);

                Module.SaveOrUpdate();
                trans.Commit();
            }
            return Json(new { RedirectUrl = returnUrl, ErrorMessage = string.Empty });
        }

        protected override Dictionary<String, String> GetDefaultModuleActions()
        {
            Dictionary<String, String> result = base.GetDefaultModuleActions();
            result.Add(JSC.Portal.Web.App.Resources.Msg.Edit, 
                Url.Action("Edit", ModuleControllerNames.STATICCONTENTS_CONTROLLER, new { sectionid = Section.Id, returnUrl = Request.Url.PathAndQuery }));
            return result;
        }
    }
}
